import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws SQLException {

        //1.创建并初始化数据源
        DataSource dataSource = new MysqlDataSource();

        //2.与数据库服务器建立连接
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("st730930");
        Connection connection = dataSource.getConnection();

        //3.构造sql语句
        String sql1 = "insert into student values(101,'张三'),(102,'李四'),(103,'王五')";
        String sql2 = "select * from student;";

        //4.执行sql语句
        PreparedStatement statement1 = connection.prepareStatement(sql1);
        PreparedStatement statement2 = connection.prepareStatement(sql2);
        int ret = statement1.executeUpdate();
        System.out.println("修改了"+ret+"信息");//显示修改信息

        //5.遍历结果集
        ResultSet resultSet = statement2.executeQuery();
        System.out.println("Student表信息:");
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = "+id+" name = "+name);
        }

        //6.释放资源
        statement2.close();
        statement1.close();
        connection.close();
    }
}
